/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */

package org.jaxson.struts.broadcast;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.config.Result;
import org.jaxson.struts.BaseAction;
import org.jaxson.struts.reverseAjax.ReverseAjaxEvent;
import org.jaxson.struts.reverseAjax.ReverseAjaxManager;
import org.jaxson.util.web.Sessions;

/**
 * Action for the admin screen which posts Broadcast Messages to all clients.
 * 
 * @author Joe Maisel
 */
@Result(name = "success", value = "/admin/broadcast-message.jsp")
public class BroadcastMessageAction extends BaseAction
{
	private BroadcastMessage msg;

	public BroadcastMessageAction ()
	{
		msg = new BroadcastMessage();
		setModel( msg );
	}

	/**
	 * @see org.jaxson.struts.BaseAction#handle()
	 */
	@Override
	public String handle()
	{

		try
		{
			if( msg != null && msg.getMessage() != null )
			{

				List<HttpSession> sessions = Sessions.getAllSessions();

				for( HttpSession session : sessions )
				{
					ReverseAjaxManager.sendImmediately( session, (ReverseAjaxEvent) getModel() );
				}

			}
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}

		return "success";
	}

	private static final long serialVersionUID = 3837490837498471L;
}
